package org.example.big_event.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.example.big_event.mapper.ArticleMapper;
import org.example.big_event.pojo.Article;
import org.example.big_event.pojo.Category;
import org.example.big_event.pojo.PageBean;
import org.example.big_event.pojo.Result;
import org.example.big_event.service.ArticleService;
import org.example.big_event.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }


    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.update(article);
        return Result.success();
    }

    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article  a = articleService.findByArticleId(id);
        if(a!=null){
            return Result.success(a);
        }
        return Result.error("文章不存在");
    }

    @DeleteMapping
    public Result delete(Integer id){
        Article a = articleService.findByArticleId(id);
        //校验文章是否存在
        if (a!=null){
            articleService.delete(id);
            return Result.success();
        }
        else  {
            return Result.error("文章不存在");
        }
    }
}
